package cn.zzu.Etrip.all_buyCar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.etrip.R;
import java.util.ArrayList;
import java.util.HashMap;
import cn.zzu.Etrip.oneday.activity.oneday_database;
import cn.zzu.Etrip.twoday.service.lightAndPrice;
/**
 * Created by ASUS on 2018/6/18.
 */
public class BuyCar extends AppCompatActivity {
    ListView listView = null;
    //***********************************************----------------**/
    static ArrayList<String> timeArray = null;
    static ArrayList<String> priceArray = null;
    //声明一个list数组装一些适配器的map；
    final ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    int bianhao = 1;
    Button button = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buy_car_list);
        listView = findViewById(R.id.buy_car_list);
        //建立适配器
        buildAdapter(listView);
        //为ListView添加一个监听器事件
        setListener(listView);
    }
    //设立监听器监听器方法
    public void setListener(ListView listView) {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                //获取适配器的所有Model,通过position指定获取哪一个model
                parent.getAdapter().getItem(position);
                //得到价格
                final TextView text = view.findViewById(R.id.price);
                //获取一个订单的文本
                final TextView dingdan = view.findViewById(R.id.button);
                //获取list的position得到第几个模板
                final int number = position;
                //得到一个对话弹框
                final AlertDialog.Builder pay = new AlertDialog.Builder(BuyCar.this);
                pay.setTitle("马上订购");
                pay.setMessage("☆☆☆请确定订购☆☆☆");
                pay.setIcon(R.mipmap.a);
                pay.setNegativeButton("关闭",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(BuyCar.this, "一定要来回来哦✈", Toast.LENGTH_SHORT).show();
                            }
                        });
                //当点击确定之后触发的事件
                pay.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                AlertDialog.Builder finalResult = new AlertDialog.Builder(BuyCar.this);
                                finalResult.setMessage("☺☺☺☺成功☺☺☺☺");
                                //提醒顾客已经购买
                                dingdan.setText("已订购");
                                dingdan.setTextColor(getResources().getColor(R.color.main_colorBlue));
                                finalResult.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent(BuyCar.this, oneday_database.class);
                                        intent.putExtra("目的地", lightAndPrice.place_name[number]);
                                        intent.putExtra("singleprice", Integer.parseInt(text.getText().subSequence(text.getText().toString().indexOf("￥") + 1, text.getText().toString().indexOf("元")).toString()));
                                        intent.putExtra("number", (getIntent().getExtras().getInt("radioButton")==0)|| (getIntent().getExtras().getInt("radioButton")==1)?1:getIntent().getExtras().getInt("radioButton",1));
                                        /***************当activity完成之后，会从栈中消失**********************/
                                        finish();
                                        startActivity(intent);
                                        list.remove(number);
                                        timeArray.remove(number);
                                        priceArray.remove(number);
                                        Toast.makeText(BuyCar.this, dingdan.getText(), Toast.LENGTH_SHORT).show();
                                        ///这里有一个bug就是当返回时，一切就会复原，想办法将从list中移除
                                    }
                                });
                                finalResult.create();
                                finalResult.show();
                            }
                        });
                pay.show();
            }
        });
    }

    //为listview 建立一个item适配器
    public void buildAdapter(ListView listView) {
        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        String date=null;
        String price=null;
        if( !(bundle.get("date").equals("0"))&&!(bundle.get("price").equals("0"))){
             date = (String) bundle.get("date");
             price= (String) bundle.get("price");
             //单例化时间和金钱的数组
            if (timeArray == null && priceArray == null) {
                timeArray = new ArrayList<String>();
                priceArray = new ArrayList<String>();
            }
            //当价格和时间不为空且不为0时就将放入数组
            if(date!=null||price!=null) {
                timeArray.add(date);
                priceArray.add(price);
            }
        }
        //规定当时间和钱同时为0时不放map
        if((timeArray!=null)&&(priceArray!=null)){
            for (int number = 0; number < priceArray.size(); number++) {
                //建立一个储存数据的map
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("bianhao", "A" + bianhao++);
                map.put("date", timeArray.get(number));
                map.put("price", priceArray.get(number));
                list.add(map);
            }
        }

        //声明一个适配器
        SimpleAdapter adapter = new SimpleAdapter(BuyCar.this, list, R.layout.buy_car, new String[]{"date", "price", "bianhao"}, new int[]{R.id.currentTime, R.id.price, R.id.value});
        listView.setAdapter(adapter);
    }


}
